package osiris.utils;

import java.io.File;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import osiris.Crypton;

public class OsirisXMLUtils {

	 public static String getValueFromFile(File xmlFile, String tagName, String paramType, String paramName)
     {
             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance ( );
             Document xmlDocument = null;

             try
             {
                     
                     DocumentBuilder builder = dbFactory.newDocumentBuilder();
                     xmlDocument = builder.parse( xmlFile );
                     NodeList listServersNodes=xmlDocument.getElementsByTagName(tagName);
                     
                     for(int i=0;i<listServersNodes.getLength();i++){
                             NamedNodeMap attributes = listServersNodes.item(i).getAttributes();
                             String type=attributes.getNamedItem("TYPE").getNodeValue();
                             String name=attributes.getNamedItem("NAME").getNodeValue();
                             if (type.equalsIgnoreCase(paramType)&&name.equalsIgnoreCase(paramName)){
                                     String crypted=attributes.getNamedItem("CRYPTED").getNodeValue();
                                     if (crypted.equalsIgnoreCase("YES")){
                                             return Crypton.decrypt(attributes.getNamedItem("VALUE").getNodeValue());
                                     }else{
                                             return attributes.getNamedItem("VALUE").getNodeValue();
                                     }
                             }
                      } 
                     return null;
             }
             catch (Exception spe)
             {
                spe.printStackTrace();
                return null;
             }
             }
     
     public static Hashtable<NamedNodeMap, String >getTagListFromFile(File xmlFile, String tagName)
     {
             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance ( );
             Document xmlDocument = null;
             
             Hashtable<NamedNodeMap, String > retAttributes=new Hashtable<NamedNodeMap, String >();
             
             try
             {
                     
                     DocumentBuilder builder = dbFactory.newDocumentBuilder();
                     xmlDocument = builder.parse( xmlFile );
                     NodeList listServersNodes=xmlDocument.getElementsByTagName(tagName);
                     
                     for(int i=0;i<listServersNodes.getLength();i++){
                             NamedNodeMap attributes = listServersNodes.item(i).getAttributes();
                             retAttributes.put(attributes, listServersNodes.item(i).getTextContent());
                     } 
                     return retAttributes;
             }
             catch (Exception spe)
             {
                spe.printStackTrace();
                return null;
             }
             }
	
}
